home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / camMethods.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.7 KB  |  379 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include "gl.h"
  22. #include "get.h"
  23. #include "device.h"
  24. #include "geom.h"
  25. #include "class.h"
  26. #include "classIds.h"
  27. #include "selectors.h"
  28. #include "mbox.h"
  29. #include "individual.h"
  30. #include "behavior.h"
  31. #include "doers.h"
  32. #include "panel.h"
  33. #include "pick.h"
  34. #include "camera.h"
  35. #include "colors.h"
  36. #include "bub.h"
  37.  
  38. extern individual *us;
  39. extern masterPanel *thePanel;
  40. extern int controls, allbuttons;
  41. extern point2d curWindow;
  42.  
  43. char frozen  = FALSE;
  44. char bullet  = FALSE;
  45. char timeit = FALSE;
  46. char warping = FALSE;
  47. char quit    = FALSE;
  48. char hz30;
  49. char dobackground = 3;        /* >0 if need to redraw background */
  50.  
  51. void setCameraView(individual*);
  52. void refreshBowl(camera*);
  53.  
  54.  
  55. take(s)
  56.     register individual *s;
  57. {
  58.     static point last;
  59.     static int lastzoom = 0;
  60.     static int moving = 0;
  61.     static int wasmoving = 0;
  62.  
  63.     setCameraView(s);
  64.  
  65.     if ((s->position.x != last.x) || (s->rotation.y != last.y) ||
  66.        (s->rotation.z != last.z) || (((camera *)s)->zoom != lastzoom)) {
  67.     fishWritemask(BACKGROUND);
  68.     fishColor(WATER_COLOR);
  69.     clear();
  70.     moving = TRUE;
  71.     }
  72.     else moving = FALSE;
  73.  
  74.     if (lastzoom == 0) {
  75.     fishWritemask(BACKGROUND);
  76.     fishColor(WATER_COLOR);
  77.     clear();
  78.     dobackground = 3;    /* clear bad initial frame on startup */
  79.     }
  80.  
  81.     if ((moving == FALSE) && (wasmoving == TRUE)) {
  82.     dobackground = 2;    /* redraw background */
  83.     }
  84.  
  85.     last.x = s->position.x;
  86.     last.y = s->rotation.y;
  87.     last.z = s->rotation.z;
  88.     lastzoom = ((camera *)s)->zoom;
  89.     wasmoving = moving;
  90. }
  91.  
  92. /* ARGSUSED */
  93. leftright(s,argtype,incr)
  94.     register individual *s;
  95.     long argtype;
  96.     long incr;
  97. {
  98.     /* printf("leftright: %d <= %d\n",s->rotation.z, incr); /**/
  99.     s->rotation.z = incr;
  100. }
  101.  
  102. /* ARGSUSED */
  103. updown(s,argtype,incr)
  104.     register individual *s;
  105.     long argtype;
  106.     long incr;
  107. {
  108.     /* printf("updown: %d <= %d\n",s->rotation.y, incr); /**/
  109.     s->rotation.y = incr;
  110. }
  111.  
  112. /* ARGSUSED */
  113. inout(s,argtype,incr)
  114.     register individual *s;
  115.     long argtype;
  116.     long incr;
  117. {
  118.     /* printf("inout: %d <= %d\n",s->position.x, incr); /**/
  119.     s->position.x = incr;
  120. }
  121.  
  122. /* ARGSUSED */
  123. zoom(s,argtype,incr)
  124.     register camera *s;
  125.     long argtype;
  126.     long incr;
  127. {
  128.         /* printf("zoom: %d <= %d\n",s->zoom, incr); /**/
  129.     s->zoom = incr;
  130.     if(s->zoom < 2)
  131.     s->zoom = 2;
  132.     if(s->zoom > 1800)
  133.     s->zoom = 1800;
  134. }
  135.  
  136.     /*
  137.      *  restore default viewing parameters for camers
  138.      */
  139. home(s)
  140.     register individual *s;
  141. {
  142.     s->position.x = 10000;
  143.     s->position.y = 10000;
  144.     s->position.z = 10000;
  145.     s->rotation.x = 0;
  146. #ifdef LONELY
  147.     s->rotation.y = 0;
  148. #else  /* LONELY */
  149.     s->rotation.y = 900;
  150. #endif /* LONELY */
  151.     s->rotation.z = 0;
  152.     ((camera *)s)->zoom = 900;
  153. }
  154.  
  155. getMouse(s)
  156.     register camera *s;
  157. {
  158.     register long dev = 0;
  159.     static short val;
  160.     long mx, my;
  161.     char picking = FALSE;
  162.     long nhit;
  163.     short hitlist[HITENTRIES];
  164.     hitstruct hit;
  165.     Object pickObj;
  166.     long penup = FALSE;
  167.  
  168.     static long downdev   = 0;
  169.     static long lastval  = 0;
  170. #ifdef DEBUG
  171.     static long lastx, lasty;
  172. #endif
  173.     static Object lastObj = NULL;
  174.  
  175.     if (qtest()) {
  176.     if ((dev = qread(&val)) == MIDDLEMOUSE ||
  177.             dev == LEFTMOUSE ||
  178.             dev == RIGHTMOUSE ||
  179.             dev == BKEY ||
  180.             dev == FKEY ||
  181.             dev == QKEY ||
  182.             dev == SKEY ||
  183.             dev == ZKEY ||
  184.             dev == VIRGULEKEY ||
  185.             dev == ESCKEY ) {
  186.         if (val == 0) {
  187.         switch (dev) {
  188.             case RIGHTMOUSE:
  189.             case MIDDLEMOUSE:
  190.             case LEFTMOUSE:
  191.             if (lastval == 1)
  192.                  penup = TRUE;  /* to downdev for pen-up process */
  193.             else downdev = 0;
  194.             break;
  195.             case ZKEY:
  196.             warping = FALSE;
  197.             break;
  198.             default:
  199.             break;
  200.         }
  201.         }
  202.         else if (val == 1) {
  203.         switch (dev) {
  204.             case RIGHTMOUSE:
  205.             case MIDDLEMOUSE:
  206.             case LEFTMOUSE:
  207.             downdev = dev;
  208.             break;
  209.             case QKEY:
  210.             home(s);    /* restore default viewing parameters */
  211.             break;
  212.             case FKEY:
  213.             frozen = frozen ? 0 : 1;
  214.             break;
  215.             case BKEY:
  216.             bullet = bullet ? 0 : 1;
  217.             break;
  218.             case ESCKEY:
  219.             quit = quit ? 0 : 1;
  220.             break;
  221.             case VIRGULEKEY:
  222.             timeit = timeit ? 0 : 1;
  223.             break;
  224.             case SKEY:
  225.             if(hz30)
  226.                 setmonitor(HZ30);
  227.             else
  228.                 setmonitor(HZ60);
  229.             hz30 = !hz30;
  230.             break;
  231.             case ZKEY:
  232.             warping = TRUE;
  233.             break;
  234.             default:
  235.             break;
  236.         }
  237.         }
  238.     }
  239.     }
  240.     if (downdev) {
  241.     mx = getvaluator(MOUSEX);
  242.     my = getvaluator(MOUSEY);
  243.     switch (downdev) {
  244.         /*********************************** the way it was ***********
  245.         case LEFTMOUSE:
  246.         Msg(s,LEFTRIGHT, INTARG,
  247.             (long)((mx - (float)(XMAXSCREEN/2))/ XMAXSCREEN * 3600));
  248.         Msg(s,UPDOWN, INTARG,
  249.             (long)((float)(YMAXSCREEN - my)/ YMAXSCREEN * 1800));
  250.         break;
  251.         case MIDDLEMOUSE:
  252.         if (warping)
  253.              Msg(s,INOUT, INTARG,
  254.             (long)((float)(YMAXSCREEN - my)/ YMAXSCREEN * 20000));
  255.         else Msg(s,ZOOM, INTARG,
  256.             (long)((float)(YMAXSCREEN - my)/ YMAXSCREEN * 1800));
  257.         break;
  258.         case RIGHTMOUSE:
  259.         conduct the hit processing shown under MIDDLEMOUSE below
  260.         break;
  261.         ************************************ the way it was *************/
  262.  
  263.         case LEFTMOUSE:
  264.         case RIGHTMOUSE:
  265.             /*************
  266.         if (! allbuttons)
  267.             break;
  268.             /**********/
  269.         case MIDDLEMOUSE:
  270.             /* conduct hit processing */
  271.         pushmatrix();
  272.             /* BUG: see refreshBowl */
  273.             viewport(0,curWindow.y, 0,curWindow.y);
  274.             pick(hitlist, NNAMES);
  275.             refreshBowl(s);    /* refresh bowl's viewport contents  */
  276.             nhit = gethit(&hit, hitlist, endpick(hitlist));
  277.                 /* hit object is first item in hitlist */
  278.             pickObj = (nhit >0) ? hit.hitlist[0] : NULL;
  279.             if(pickObj == NULL) {
  280.             /* BUG: see refreshControls */
  281.             pushviewport();
  282.             viewport(curWindow.y, curWindow.x, 0, curWindow.y);
  283.             pick(hitlist, NNAMES);
  284.             refreshControls();    /* panel's viewport contents */
  285.             nhit = gethit(&hit, hitlist, endpick(hitlist));
  286.                 /* hit object is first item in hitlist */
  287.             pickObj = (nhit >0) ? hit.hitlist[0] : NULL;
  288.             popviewport();
  289.             }
  290.             hit.x = mx;
  291.             hit.y = my;
  292.         popmatrix();
  293.  
  294.             /* look for button state and hit Object changes */
  295.         if (lastval == 0 && val == 1)
  296.              picking = BEGINSELECT;
  297.         else if (lastval == 1 && val == 0)
  298.              picking = ENDSELECT;
  299.         else if (lastObj == pickObj)
  300.              picking = CONTSELECT;
  301.         else {
  302.              if (lastObj)
  303.              Msg(lastObj, OLDSELECT, HITARG, &hit);
  304.              picking = NEWSELECT;
  305.         }
  306.  
  307.         if (nhit && pickObj) {
  308.             /* direct input to the picked object */
  309.             /* printf("pick: sent %d to 0x%x\n", picking, pickObj);/* */
  310.             Msg(pickObj, picking, HITARG, &hit);
  311.         }
  312.         else {
  313.             /* direct input which misses all other objects to US */
  314.             /* printf("pick: sent %d to US\n", picking); /* */
  315.             Msg(us, picking, HITARG, &hit);
  316.         }
  317.         break;
  318.     }
  319. #ifdef DEBUG
  320.     lastx   = mx;
  321.     lasty   = my;
  322. #endif
  323.     lastval = val;
  324.     lastObj = pickObj;
  325.     }
  326.     else if (val == 0) lastval = val;
  327.  
  328.     if (penup) {
  329.     penup = FALSE;
  330.     downdev = 0;        /* end of pen-up event */
  331.     }
  332.  
  333. }
  334.  
  335.     /*
  336.      *  setup viewing transformation from camera's information
  337.      */
  338. void
  339. setCameraView(s)
  340.     register individual *s;
  341. {
  342.     perspective(((camera *)s)->zoom,1.0,100.0,110000.0);
  343.     polarview((float)s->position.x,s->rotation.z,s->rotation.y,0);
  344. }
  345.     /*
  346.      *  refresh bowl's viewport contents for picking purposes
  347.      */
  348. void
  349. refreshBowl(camera* c)
  350. {
  351.     setCameraView((individual*)c);
  352.     /* BUG: cant change viewport durring picking */
  353.     /* viewport(0,curWindow.y, 0,curWindow.y); /* */
  354.     pushmatrix();
  355.     callobj((Object)us);
  356.     popmatrix();
  357. }
  358.  
  359.     /*
  360.      *  refresh control panel's viewport contents for picking purposes
  361.      */
  362. refreshControls()
  363. {
  364.     if (controls) {
  365.     /* pushviewport(); /* */
  366.         /* BUG: cant change viewport durring picking */
  367.         /* viewport(curWindow.y, curWindow.x, 0, curWindow.y); /* */
  368. /******
  369.         gl_makepickmat();
  370. /******/
  371.         ortho2(0.0, 256.0, 767.0, 0.0);
  372.         pushmatrix();
  373.         backface(FALSE);
  374.         callobj((Object)thePanel);
  375.         popmatrix();
  376.     /* popviewport(); /* */
  377.     }
  378. }
  379.